﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DEMO3.BUS;
using DEMO3.DTO;
namespace DEMO3.GUI
{
    public partial class SuatChieu : UserControl
    {
        public SuatChieu()
        {
            InitializeComponent();
        }
        private void SuatChieu_Load(object sender, EventArgs e)
        {
            DataProvider test = new DataProvider();
            if (!test.OpenConnect())
                Main.Warning();
            else
                LoadData();
            test.CloseConnect();
        }
        private void LoadData()
        {
            SuatChieuBUS bus = new SuatChieuBUS();
            gc.DataSource = bus.Get();
            DataProvider test = new DataProvider();
            cbxMaCaChieu.DataSource = test.ExcuteQuery("GET_CACHIEU",null);
            cbxMaCaChieu.DisplayMember = "MaCaChieu";
            cbxMaCaChieu.ValueMember = "MaCaChieu";

            cbxMaPhong.DataSource = test.ExcuteQuery("GET_PHONGCHIEU", null);
            cbxMaPhong.DisplayMember = "MaPhong";
            cbxMaPhong.ValueMember = "MaPhong";

            cbxMaPhim.DataSource = test.ExcuteQuery("GET_PHIM", null);
            cbxMaPhim.DisplayMember = "TenPhim";
            cbxMaPhim.ValueMember = "MaPhim";

        }

        private void btThem_Click(object sender, EventArgs e)
        {
            SuatChieuDTO dto = new SuatChieuDTO();
            dto.MaCaChieu = cbxMaCaChieu.SelectedValue.ToString();
            dto.MaPhim = int.Parse(cbxMaPhim.SelectedValue.ToString());
            dto.MaPhong = int.Parse(cbxMaPhong.SelectedValue.ToString());
            dto.GiaVe = int.Parse(txtGiaVe.Text);
            SuatChieuBUS bus = new SuatChieuBUS();
            bus.Add(dto);
            LoadData();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            SuatChieuDTO dto = new SuatChieuDTO();
            dto.MaSuatChieu = int.Parse(gv.GetRowCellValue(gv.FocusedRowHandle, "MaSuatChieu").ToString());
            SuatChieuBUS bus = new SuatChieuBUS();
            bus.Delete(dto);
            LoadData();
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            SuatChieuDTO dto = new SuatChieuDTO();
            dto.MaCaChieu = cbxMaCaChieu.SelectedValue.ToString();
            dto.MaPhim = int.Parse(cbxMaPhim.SelectedValue.ToString());
            dto.MaPhong = int.Parse(cbxMaPhong.SelectedValue.ToString());
            dto.GiaVe = int.Parse(txtGiaVe.Text);
            dto.MaSuatChieu = int.Parse(gv.GetRowCellValue(gv.FocusedRowHandle, "MaSuatChieu").ToString());
            SuatChieuBUS bus = new SuatChieuBUS();
            bus.Update(dto);
            LoadData();
        }

        private void gv_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gv.FocusedRowHandle != gv.RowCount && gv.RowCount != 0)
            {
               cbxMaCaChieu.SelectedValue = gv.GetRowCellValue(gv.FocusedRowHandle, "MaCaChieu").ToString();
                cbxMaPhim.SelectedValue = gv.GetRowCellValue(gv.FocusedRowHandle, "MaPhim").ToString();
                cbxMaPhong.SelectedValue = gv.GetRowCellValue(gv.FocusedRowHandle, "MaPhong").ToString();
                txtGiaVe.Text = string.Format("{0:###}",gv.GetRowCellValue(gv.FocusedRowHandle, "GiaVe"));
            }
        }



    }
}
